#include <iostream>
#include <string>
#include <vector>

#include <ctime>
#include <cstdlib>

using namespace std;

void pr_3_21()
{
    vector<int> v1;
    vector<int> v2(10);
    vector<int> v3(10, 42);
    vector<int> v4{10};
    vector<int> v5{10, 42};
    vector<string> v6{10};
    vector<string> v7{10, "hi"};

    for (auto it = v1.begin(); it != v1.end(); ++it)
    {
        cout << *it << " ";
    }
    cout << endl;

    for (auto it = v2.begin(); it != v2.end(); ++it)
    {
        cout << *it << " ";
    }
    cout << endl;

    for (auto it = v3.begin(); it != v3.end(); ++it)
    {
        cout << *it << " ";
    }
    cout << endl;

    for (auto it = v4.begin(); it != v4.end(); ++it)
    {
        cout << *it << " ";
    }
    cout << endl;

    for (auto it = v5.begin(); it != v5.end(); ++it)
    {
        cout << *it << " ";
    }
    cout << endl;

    for (auto it = v6.begin(); it != v6.end(); ++it)
    {
        cout << *it << " ";
    }
    cout << endl;

    for (auto it = v7.begin(); it != v7.end(); ++it)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void pr_3_22()
{
    vector<string> text;
    string s;
    // 利用 getline 读取一句话，直接回车产生一个空串，表示段落结束
    while (getline(cin, s))
    {
        text.push_back(s); // 逐个添加到text中
    }
    // 利用迭代器遍历全部字符串，遇空串停止循环
    for (auto it = text.begin(); it != text.end() && !it->empty(); it++)
    {
        // 利用迭代器遍历当前字符串
        for (auto it2 = it->begin(); it2 != it->end(); it2++)
        {
            *it2 = toupper(*it2); // 改写成大写形式
        }
        cout << *it << endl; // 输出当前字符串
    }
}

void pr_3_23()
{
    vector<int> vInt;
    srand((unsigned)time(NULL)); // 生成随机数种子
    for (int i = 0; i < 10; i++) // 循环10次
    {
        // 每次循环生成一个 1000以内的随机数并添加到 vInt中
        vInt.push_back(rand() % 1000);
    }
    cout << "rand 10 numbers is: " << endl;
    // 利用常量迭代器读取原始数据
    for (auto it = vInt.cbegin(); it != vInt.cend(); it++)
    {
        cout << *it << " "; // 输出当前数字
    }
    cout << endl;
    cout << "Fan Bei Hou 10 ShuZi: " << endl;
    // 利用非常量迭代器修改 vInt内容并输出
    for (auto it2 = vInt.begin(); it2 != vInt.end(); it2++)
    {
        *it2 *= 2;
        cout << *it2 << " "; // 输出当前数字
    }
    cout << endl;
}

int pr_3_24_1()
{
    vector<int> iVec;
    int iVal;
    cout << "Please input int value:" << endl;
    while (cin >> iVal)
    {
        iVec.push_back(iVal);
    }
    if (iVec.cbegin() == iVec.cend()) // 判定为空
    {
        cout << "no element in vector!!!" << endl;
        return -1;
    }
    cout << "cla 1 is: " << endl;
    // 注意这里的结束循环条件是 <
    for (auto it = iVec.cbegin(); it < iVec.cend() - 1; it++)
    {
        cout << (*it + *(++it)) << " "; // 相邻两个元素的和
        if ((it - iVec.cbegin() + 1) % 10 == 0)
        {
            cout << endl;
        }
    }
    // 如果是数量是奇数，对末尾元素特殊处理
    if (iVec.size() % 2 != 0)
    {
        cout << *(iVec.cend() - 1) << endl;
    }
    return 0;
}

int pr_3_24_2()
{
    vector<int> iVec;
    int iVal;
    cout << "Please input int value:" << endl;
    while (cin >> iVal)
    {
        iVec.push_back(iVal);
    }
    if (iVec.cbegin() == iVec.cend()) // 判定为空
    {
        cout << "no element in vector!!!" << endl;
        return -1;
    }
    cout << "cla 2 is: " << endl;
    auto beg = iVec.cbegin();
    auto end = iVec.cend();
    for (auto it = beg; it != beg + (end - beg) / 2; it++)
    {
        // 首尾两项的和
        cout << (*it + *(beg + (end - it) - 1)) << " ";
        // 每行输出 5个数字
        if ((it - beg + 1) % 5 == 0 )
        {
            cout << endl;
        }
    }
    // 如果元素个数是奇数，单独处理最后一个元素
    if (iVec.size() % 2 != 0)
    {
        cout << *(beg + (end - beg) / 2);
    }
    return 0;
}

void pr_3_25()
{
    vector<unsigned> scores(11, 0);
    unsigned grade;
    auto beg = scores.begin();
    cout << "please input scores: " << endl;
    while (cin >> grade)
    {
        if (grade <= 100)
        {
            ++*(beg + grade / 10);
        }
    }
    for (auto it = scores.cbegin(); it != scores.cend(); it++)
    {
        cout << *it << " ";
    }
}

int main()
{
    pr_3_24_1();
    return 0;
}
